package com.yourenbang.lingyun.transaction.web.service.impl.fund;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import com.yourenbang.lingyun.constants.AccountStatus;
import com.yourenbang.lingyun.constants.BizCodeMessages;
import com.yourenbang.lingyun.constants.enums.ChannelIdEnums;
import com.yourenbang.lingyun.constants.enums.FundBillType;
import com.yourenbang.lingyun.constants.enums.PlatNoEnums;
import com.yourenbang.lingyun.constants.enums.ShareLevelBaseEnums;
import com.yourenbang.lingyun.constants.enums.fund.AccountMainJournalType;
import com.yourenbang.lingyun.constants.enums.fund.AccountType;
import com.yourenbang.lingyun.core.config.datasource.DataSources;
import com.yourenbang.lingyun.core.config.datasource.RoutingDataSource;
import com.yourenbang.lingyun.core.message.Result;
import com.yourenbang.lingyun.core.util.BizAssertUtils;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.core.util.NumberUtil;
import com.yourenbang.lingyun.core.util.StringUtils;
import com.yourenbang.lingyun.core.util.date.DateFormat;
import com.yourenbang.lingyun.core.util.date.LocalDateUtils;
import com.yourenbang.lingyun.core.util.encrypt.AesEncryptUtil;
import com.yourenbang.lingyun.domain.CreditBuyOrder;
import com.yourenbang.lingyun.domain.CreditBuyOrderExample;
import com.yourenbang.lingyun.domain.FundAccount;
import com.yourenbang.lingyun.domain.UserBaseExt;
import com.yourenbang.lingyun.mapper.CreditBuyOrderMapper;
import com.yourenbang.lingyun.mapper.FundAccountMapper;
import com.yourenbang.lingyun.transaction.config.AcmTransactionParams;
import com.yourenbang.lingyun.transaction.constant.WalletList;
import com.yourenbang.lingyun.transaction.mapper.*;
import com.yourenbang.lingyun.transaction.util.DateUtil;
import com.yourenbang.lingyun.transaction.web.domain.dto.fund.*;
import com.yourenbang.lingyun.transaction.web.domain.vo.fund.AccountQueryGroupVO;
import com.yourenbang.lingyun.transaction.web.domain.vo.fund.AccountQueryVO;
import com.yourenbang.lingyun.transaction.web.domain.vo.fund.AllyHomePageVO;
import com.yourenbang.lingyun.transaction.web.domain.vo.fund.MerAccountQueryGroupVO;
import com.yourenbang.lingyun.transaction.web.domain.vo.pay.WithdrawInAmountFeeVO;
import com.yourenbang.lingyun.transaction.web.domain.vo.score.ScoreQueryGroupVO;
import com.yourenbang.lingyun.transaction.web.service.fund.AccountService;
import com.yourenbang.lingyun.transaction.web.service.impl.pay.PayAccountConfig;
import com.yourenbang.lingyun.transaction.web.service.impl.pay.withdraw.WithdrawAccountConfig;
import com.yourenbang.settlement.api.service.SettlementRpcService;
import com.yourenbang.transaction.api.model.dto.AddAccountDTO;
import com.yourenbang.transaction.api.model.dto.fund.FundQueryDTO;
import com.yourenbang.transaction.api.model.vo.LingYunAccVO;
import com.yourenbang.transaction.api.model.vo.fund.FundQueryVO;
import com.yourenbang.transaction.api.service.fund.FundApiService;
import com.yourenbang.ucenter.api.model.dto.SuperiorAllyDTO;
import com.yourenbang.ucenter.api.service.AllyProfitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {
    private static final Interner<String> pool = Interners.newWeakInterner();

    @Autowired
    private AcmTransactionParams acmTransactionParams;
    @Autowired
    private FundAccountMapper fundAccountMapper;
    @Autowired
    private TransactionFundAccountMapper transactionFundAccountMapper;
    @Autowired
    private TransactionFundAccountWithdrawPaymentMapper transactionFundAccountWithdrawPaymentMapper;
    @Autowired
    private TransactionFundBillMapper transactionFundBillMapper;
    @Autowired
    private TransactionUserMapper transactionUserMapper;
    @Autowired
    private FundApiService fundApiService;
    @Autowired
    private AllyProfitService allyProfitService;
    @Autowired
    private TransactionAllyActivateRewardMapper transactionAllyActivateRewardMapper;
    @Autowired
    private SettlementRpcService settlementRpcService;
    @Resource
    private CreditBuyOrderMapper creditBuyOrderMapper;

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public Result addAccount(AddAccountDTO dto) {

        synchronized (pool.intern("addFundAccount" + dto.getUserBaseId())) {
            FundAccount fundAccount = transactionFundAccountMapper.getUserAccountByType(dto.getUserBaseId(),
                    dto.getAccountType());
            if (Objects.nonNull(fundAccount)) {
                return Result.builder().success(fundAccount);
            }

            KLog.info("[transaction创建资金账户]资金账户为null, 开始创建账户userId:{}", dto.getUserBaseId());
            fundAccount = new FundAccount();
            fundAccount.setAccountName(dto.getAccountName());
            fundAccount.setUserBaseId(dto.getUserBaseId());
            fundAccount.setAccountType(dto.getAccountType());
            fundAccount.setAccountTypeName(dto.getAccountTypeName());
            fundAccount.setAccountIncome(BigDecimal.ZERO);
            fundAccount.setAccountAmountFrozen(BigDecimal.ZERO);
            fundAccount.setAccountAmountWithdrawable(BigDecimal.ZERO);
            fundAccount.setAccountAmountWithdrawed(BigDecimal.ZERO);
            fundAccount.setAccountAmountUndepositable(BigDecimal.ZERO);
            fundAccount.setAccountStatus(AccountStatus.normal);
            fundAccount.setVersion(0);
            fundAccount.setChannelId(dto.getChannelId());
            fundAccount.setPlatNo(dto.getPlatNo());
            if (Objects.nonNull(dto.getCreateTime())) {
                fundAccount.setCreateTime(dto.getCreateTime());
            }
            if (Objects.nonNull(dto.getUpdateTime())) {
                fundAccount.setUpdateTime(dto.getUpdateTime());
            }
            int insert = fundAccountMapper.insertSelective(fundAccount);
            BizAssertUtils.assertOne(insert, BizCodeMessages.DB_WRITE_ERR, "创建资金账户失败");
            Result<FundAccount> result = Result.builder().success();
            result.setData(fundAccount);
            KLog.info("[transaction创建资金账户]资金账户为null,创建账户结果:{}", result.toString());
            return result;
        }

    }

    /**
     * 用户账户查询
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<AccountQueryVO> queryAccount(AccountQueryDTO accountQueryDTO) {
        KLog.info("【用户账户查询】入参:{}", accountQueryDTO.toJSON());
        AccountQueryVO queryVO = new AccountQueryVO();
        queryVO.setCreditFlag(acmTransactionParams.getCreditFlag());
        queryVO.setShoppingFlag(acmTransactionParams.getShoppingFlag());
        queryVO.setAllyReachFlag(acmTransactionParams.getAllyReachFlag());

        FundQueryDTO fundQueryDTO = new FundQueryDTO();
        fundQueryDTO.setUserBaseId(accountQueryDTO.getUserBaseId());
        Result<Map<Integer, FundQueryVO>> fundAccountResult = fundApiService.queryAllFund(fundQueryDTO);
        Map<Integer, FundQueryVO> fundQueryVOMap;
        if (fundAccountResult.isSuccess()) {
            fundQueryVOMap = fundAccountResult.getData();
        } else {
            fundQueryVOMap = new HashMap<>();
        }

        /*  ***************************************************************   商户账户  ************************************************************************ */
        FundQueryVO merFund = fundQueryVOMap.get(AccountType.getFullCode(AccountType.MER, accountQueryDTO.getPlatNo()));
        if (merFund == null) {
            merFund = new FundQueryVO();
        }
        queryVO.setMerWithdrawable(merFund.getAccountWithdrawable());
        queryVO.setMerWithdrawed(merFund.getAccountWithdrawed());
        /*  ***************************************************************   盟友账户  ************************************************************************ */
        FundQueryVO allyFund = fundQueryVOMap.get(AccountType.getFullCode(AccountType.ALLY, accountQueryDTO.getPlatNo()));
        if (allyFund == null) {
            allyFund = new FundQueryVO();
        }
        queryVO.setAllyWithdrawable(allyFund.getAccountWithdrawable());
        BigDecimal allyEithdrawed = transactionFundAccountWithdrawPaymentMapper.accountWithdrawed(accountQueryDTO.getPlatNo(), AccountType.getFullCode(AccountType.ALLY, accountQueryDTO.getPlatNo()), accountQueryDTO.getUserBaseId());
        queryVO.setAllyWithdrawed(allyEithdrawed);
        if (acmTransactionParams.getHistoryAllyWithdrawedFlag()) {
            //历史已提金额
            BigDecimal historyWithdrawAmt = transactionFundBillMapper.historyWithdrawAmt(accountQueryDTO.getUserBaseId()).abs();
            queryVO.setAllyWithdrawed(queryVO.getAllyWithdrawed().add(historyWithdrawAmt));
        }
        /*  ***************************************************************   盟友达标奖励账户  ***************************************************************** */
        FundQueryVO allyReachFund = fundQueryVOMap.get(AccountType.getFullCode(AccountType.ALLY_REACH, accountQueryDTO.getPlatNo()));
        if (allyReachFund == null) {
            allyReachFund = new FundQueryVO();
        }
        queryVO.setAllyReachWithdrawable(allyReachFund.getAccountWithdrawable());
        queryVO.setAllyReachWithdrawed(allyReachFund.getAccountWithdrawed().add(allyReachFund.getAccountFrozen()));
        /*  ***************************************************************   信用卡返佣账户  ******************************************************************* */
        FundQueryVO creditFund = fundQueryVOMap.get(AccountType.getFullCode(AccountType.CREDIT_COMMISSION, accountQueryDTO.getPlatNo()));
        if (creditFund == null) {
            creditFund = new FundQueryVO();
        }
        queryVO.setCreditWithdrawable(creditFund.getAccountWithdrawable());
        queryVO.setCreditWithdrawed(creditFund.getAccountWithdrawed().add(creditFund.getAccountFrozen()));
        /*  ***************************************************************   购物积分返利账户  ****************************************************************** */
        FundQueryVO shoppingScoreFund = fundQueryVOMap.get(AccountType.getFullCode(AccountType.SHOPPING_SCORE, accountQueryDTO.getPlatNo()));
        if (shoppingScoreFund == null) {
            shoppingScoreFund = new FundQueryVO();
        }
        queryVO.setShoppingWithdrawable(shoppingScoreFund.getAccountWithdrawable());
        queryVO.setShoppingWithdrawed(shoppingScoreFund.getAccountWithdrawed());


        queryVO.setTodayIncome(transactionFundBillMapper.getAllyTodayIncome(accountQueryDTO.getPlatNo(), accountQueryDTO.getUserBaseId(), DateFormat.DATE_COMPACT.format(LocalDateTime.now()), PayAccountConfig.accountTypeTodayIncome(accountQueryDTO.getPlatNo())));
        KLog.info("【用户账户查询】出参:{}, 入参:{}", queryVO.toJSON(), accountQueryDTO.toJSON());
        return Result.builder().success(queryVO);
    }

    /**
     * 盟友账户查询
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<AccountQueryGroupVO> queryAccountGroup(AccountQueryDTO accountQueryDTO) {
        KLog.info("【盟友账户查询】入参:{}", accountQueryDTO.toJSON());
        AccountQueryGroupVO queryGroupVO = new AccountQueryGroupVO();

        FundQueryDTO fundQueryDTO = new FundQueryDTO();
        fundQueryDTO.setUserBaseId(accountQueryDTO.getUserBaseId());
        Result<Map<Integer, FundQueryVO>> fundAccountResult = fundApiService.queryAllFund(fundQueryDTO);
        Map<Integer, FundQueryVO> fundQueryVOMap;
        if (fundAccountResult.isSuccess()) {
            fundQueryVOMap = fundAccountResult.getData();
        } else {
            fundQueryVOMap = new HashMap<>();
        }

        List<FundQueryGroupVO> fundAccount = new ArrayList<>();
        for (WalletList.AllyWalletList wallet : WalletList.AllyWalletList.values()) {
            if (!wallet.isShow(accountQueryDTO.getPlatNo())) {
                continue;
            }
            FundQueryGroupVO groupVO = new FundQueryGroupVO();
            Integer accountType = Integer.parseInt(accountQueryDTO.getPlatNo() + wallet.getAccountTypeSuffix());
            groupVO.setAccountType(accountType);
            groupVO.setWalletName(wallet.getWalletName());
            FundQueryVO fundQueryVO = fundQueryVOMap.get(accountType);
            if (!Objects.isNull(fundQueryVO)) {
                groupVO.setAccountFrozen(fundQueryVO.getAccountFrozen());
                groupVO.setAccountWithdrawable(fundQueryVO.getAccountWithdrawable());
                if (WalletList.AllyWalletList.ALLY.equals(wallet)) {
                    BigDecimal allyEithdrawed = transactionFundAccountWithdrawPaymentMapper.accountWithdrawed(accountQueryDTO.getPlatNo(), accountType, accountQueryDTO.getUserBaseId());
                    groupVO.setAccountWithdrawed(allyEithdrawed);

                    if (PlatNoEnums.KLKS.isEquals(accountQueryDTO.getPlatNo())
                            && acmTransactionParams.getHistoryAllyWithdrawedFlag()) {
                        //历史已提金额
                        BigDecimal historyWithdrawAmt = transactionFundBillMapper.historyWithdrawAmt(accountQueryDTO.getUserBaseId()).abs();
                        groupVO.setAccountWithdrawed(groupVO.getAccountWithdrawed().add(historyWithdrawAmt));
                    }
                } else if (WalletList.AllyWalletList.SUB_TEAM_ACTION.equals(wallet) &&
                        fundQueryVO.getAccountWithdrawable().compareTo(BigDecimal.ZERO) == 0) {
                    //活跃度钱包余额为0 不展示
                    if (!isA7(accountQueryDTO.getUserBaseId(), accountQueryDTO.getPlatNo())) {
                        continue;
                    }
                } else {
                    groupVO.setAccountWithdrawed(fundQueryVO.getAccountWithdrawed().add(fundQueryVO.getAccountFrozen()));
                }
            } else if (WalletList.AllyWalletList.SUB_TEAM_ACTION.equals(wallet)) {
                if (!isA7(accountQueryDTO.getUserBaseId(), accountQueryDTO.getPlatNo())) {
                    continue;
                }
            }

            WithdrawInAmountFeeVO withdrawInAmountFeeVO = WithdrawAccountConfig.withdrawInAmountFeeVO(accountType);
            groupVO.setWithdrawRate(withdrawInAmountFeeVO.getWithdrawRate());
            groupVO.setWithdrawFixedAmt(withdrawInAmountFeeVO.getWithdrawFixedAmt());
            if (fmtFundQueryGroup(accountType)) {
                fundAccount.add(groupVO);
            }
        }

        List<ScoreQueryGroupVO> scoreAccount = new ArrayList<>();

        queryGroupVO.setFundAccount(fundAccount);
        queryGroupVO.setScoreAccount(scoreAccount);
        if (PlatNoEnums.YXY.isEquals(accountQueryDTO.getPlatNo())) {
            queryGroupVO.setActivateRewardAmount(NumberUtil.setDefaultScale(BigDecimal.ZERO));
            queryGroupVO.setTodayIncome(NumberUtil.setDefaultScale(BigDecimal.ZERO));
        } else {
            queryGroupVO.setTodayIncome(transactionFundBillMapper.getAllyTodayIncome(accountQueryDTO.getPlatNo(), accountQueryDTO.getUserBaseId(), DateFormat.DATE_COMPACT.format(LocalDateTime.now()), PayAccountConfig.accountTypeTodayIncome(accountQueryDTO.getPlatNo())));
        }

        KLog.info("【盟友账户查询】出参:{}, 入参:{}", queryGroupVO.toJSON(), accountQueryDTO.toJSON());
        return Result.builder().success(queryGroupVO);
    }

    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<AccountQueryGroupVO> queryAccountGroupV2(AccountQueryDTO accountQueryDTO) {

        KLog.info("【盟友账户查询V2】入参:{}", accountQueryDTO.toJSON());
        if (!PlatNoEnums.YXY.isEquals(accountQueryDTO.getPlatNo())) {
            return Result.builder().fail(BizCodeMessages.CORE_API_REQUEST_LIMITED);
        }
        AccountQueryGroupVO queryGroupVO = new AccountQueryGroupVO();

        FundQueryDTO fundQueryDTO = new FundQueryDTO();
        fundQueryDTO.setUserBaseId(accountQueryDTO.getUserBaseId());
        Result<Map<Integer, FundQueryVO>> fundAccountResult = fundApiService.queryAllFund(fundQueryDTO);


        Map<Integer, FundQueryVO> fundQueryVOMap;
        if (fundAccountResult.isSuccess() && Objects.nonNull(fundAccountResult.getData())) {
            fundQueryVOMap = fundAccountResult.getData();
            //总收益不包含充值
            BigDecimal rechargeAmt = transactionFundBillMapper.queryHistoryRechargeAmt(accountQueryDTO.getUserBaseId());
            Integer allyAllType = AccountType.getFullCode(AccountType.ALLY_V2, PlatNoEnums.YXY.getCode());
            if (fundQueryVOMap.containsKey(allyAllType)) {
                FundQueryVO fundQueryVO = fundQueryVOMap.get(allyAllType);
                fundQueryVO.setAccountIncome(fundQueryVO.getAccountIncome().subtract(rechargeAmt));
                fundQueryVOMap.put(allyAllType, fundQueryVO);
            }

            Integer bossAccountType = AccountType.getFullCode(AccountType.ALLY_FROM_BOSS, PlatNoEnums.YXY.getCode());
            if (fundQueryVOMap.containsKey(bossAccountType)) {
                FundQueryVO fundQueryVO = fundQueryVOMap.get(bossAccountType);
                fundQueryVOMap.put(bossAccountType, fundQueryVO);
            }

        } else {
            fundQueryVOMap = new HashMap<>();
        }


        //账户列表
        List<FundQueryGroupVO> fundAccount = new ArrayList<>();

        FundQueryGroupVO groupVO = new FundQueryGroupVO();
        Integer accountType = AccountType.ALLY_V2.getFullCode(accountQueryDTO.getPlatNo());
        groupVO.setAccountType(accountType);
        groupVO.setWalletName("分红收益");
        FundQueryVO fundQueryVO = fundQueryVOMap.get(accountType);
        if (Objects.nonNull(fundQueryVO)) {
            groupVO.setAccountFrozen(fundQueryVO.getAccountFrozen());
            groupVO.setAccountWithdrawable(fundQueryVO.getAccountWithdrawable());
            groupVO.setAccountWithdrawed(fundQueryVO.getAccountWithdrawed());
            groupVO.setAccountIncome(fundQueryVO.getAccountIncome());
        }

        WithdrawInAmountFeeVO withdrawInAmountFeeVO = WithdrawAccountConfig.withdrawInAmountFeeVO(accountType);
        groupVO.setWithdrawRate(withdrawInAmountFeeVO.getWithdrawRate());
        groupVO.setWithdrawFixedAmt(withdrawInAmountFeeVO.getWithdrawFixedAmt());
        if (fmtFundQueryGroup(accountType)) {
            fundAccount.add(groupVO);
        }

        FundQueryGroupVO fundQueryGroupVO = new FundQueryGroupVO();
        Integer bossAccountType = AccountType.ALLY_FROM_BOSS.getFullCode(accountQueryDTO.getPlatNo());
        fundQueryGroupVO.setAccountType(bossAccountType);
        fundQueryGroupVO.setWalletName("奖励收益");
        FundQueryVO fundQueryVO2 = fundQueryVOMap.get(bossAccountType);
        if (Objects.nonNull(fundQueryVO2)) {
            fundQueryGroupVO.setAccountFrozen(fundQueryVO2.getAccountFrozen());
            fundQueryGroupVO.setAccountWithdrawable(fundQueryVO2.getAccountWithdrawable());
            fundQueryGroupVO.setAccountWithdrawed(fundQueryVO2.getAccountWithdrawed());
            fundQueryGroupVO.setAccountIncome(fundQueryVO2.getAccountIncome());
        }

        fundQueryGroupVO.setWithdrawRate(withdrawInAmountFeeVO.getWithdrawRate());
        fundQueryGroupVO.setWithdrawFixedAmt(withdrawInAmountFeeVO.getWithdrawFixedAmt());
        if (fmtFundQueryGroup(bossAccountType)) {
            fundAccount.add(fundQueryGroupVO);
        }

        queryGroupVO.setFundAccount(fundAccount);


        BigDecimal todayIncomeV2 = transactionFundBillMapper.getAllyTodayIncomeV2(accountQueryDTO.getPlatNo(),
                accountQueryDTO.getUserBaseId(),
                DateFormat.DATE_COMPACT.format(LocalDateTime.now()));
        queryGroupVO.setTodayIncome(todayIncomeV2);
        if (PlatNoEnums.YXY.isEquals(accountQueryDTO.getPlatNo())) {
            queryGroupVO.setToBeRecordAmount(transactionAllyActivateRewardMapper.getUserToBeRecordAmount(accountQueryDTO.getPlatNo(),
                    accountQueryDTO.getUserBaseId()));
            //累加待发放的分润
            try {
                Result<BigDecimal> profitAmount = settlementRpcService.getNoPaymentProfit(accountQueryDTO.getUserBaseId());
                if (Objects.nonNull(profitAmount) && profitAmount.isSuccess()) {
                    queryGroupVO.setToBeRecordAmount(NumberUtil.setDefaultScale(queryGroupVO.getToBeRecordAmount().add(profitAmount.getData())));
                }
            } catch (Exception e) {
                KLog.error("获取待发放的分润异常， {}", e.getMessage(), e);
            }
        }

        //查询赊购订单
        CreditBuyOrderExample creditBuyOrderExample=new CreditBuyOrderExample();
        creditBuyOrderExample.createCriteria().andUserBaseIdEqualTo(accountQueryDTO.getUserBaseId());
        List<CreditBuyOrder> creditBuyOrderList = creditBuyOrderMapper.selectByExample(creditBuyOrderExample);
        if(CollectionUtils.isNotEmpty(creditBuyOrderList)){
            BigDecimal creditBuyDebt = creditBuyOrderList.stream().map(CreditBuyOrder::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add)
                    .subtract(creditBuyOrderList.stream().map(CreditBuyOrder::getAlreadyReturn).reduce(BigDecimal.ZERO, BigDecimal::add));
            queryGroupVO.setCreditBuyDebt(creditBuyDebt);
        }

        KLog.info("【盟友账户查询V2】出参:{}, 入参:{}", queryGroupVO.toJSON(), accountQueryDTO.toJSON());

        return Result.builder().success(queryGroupVO);

    }

    /**
     * 商户账户查询
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<MerAccountQueryGroupVO> queryMerAccountGroup(MerAccountQueryDTO accountQueryDTO) {
        KLog.info("【商户账户查询】入参:{}", accountQueryDTO.toJSON());
        MerAccountQueryGroupVO queryGroupVO = new MerAccountQueryGroupVO();

        FundQueryDTO fundQueryDTO = new FundQueryDTO();
        fundQueryDTO.setUserBaseId(accountQueryDTO.getUserBaseId());
        Result<Map<Integer, FundQueryVO>> fundAccountResult = fundApiService.queryAllFund(fundQueryDTO);
        Map<Integer, FundQueryVO> fundQueryVOMap;
        if (fundAccountResult.isSuccess()) {
            fundQueryVOMap = fundAccountResult.getData();
        } else {
            fundQueryVOMap = new HashMap<>();
        }

        List<FundQueryGroupVO> fundAccount = new ArrayList<>();
        for (WalletList.MerWalletList wallet : WalletList.MerWalletList.values()) {
            if (!wallet.isShow(accountQueryDTO.getPlatNo())) {
                continue;
            }
            FundQueryGroupVO groupVO = new FundQueryGroupVO();
            Integer accountType = Integer.parseInt(accountQueryDTO.getPlatNo() + wallet.getAccountTypeSuffix());
            groupVO.setAccountType(accountType);
            groupVO.setWalletName(wallet.getWalletName());
            FundQueryVO fundQueryVO = fundQueryVOMap.get(accountType);
            if (fundQueryVO != null) {
                groupVO.setAccountFrozen(fundQueryVO.getAccountFrozen());
                groupVO.setAccountWithdrawable(fundQueryVO.getAccountWithdrawable());
                groupVO.setAccountWithdrawed(fundQueryVO.getAccountWithdrawed().add(fundQueryVO.getAccountFrozen()));
            }

            WithdrawInAmountFeeVO withdrawInAmountFeeVO = WithdrawAccountConfig.withdrawInAmountFeeVO(accountType);
            groupVO.setWithdrawRate(withdrawInAmountFeeVO.getWithdrawRate());
            groupVO.setWithdrawFixedAmt(withdrawInAmountFeeVO.getWithdrawFixedAmt());
            if (fmtFundQueryGroup(accountType)) {
                fundAccount.add(groupVO);
            }
        }

        queryGroupVO.setFundAccount(fundAccount);
        KLog.info("【商户账户查询】出参:{}, 入参:{}", queryGroupVO.toJSON(), accountQueryDTO.toJSON());
        return Result.builder().success(queryGroupVO);
    }

    /**
     * 商户账户查询(快收查凌云)
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<MerAccountQueryGroupVO> queryMerAccountGroupFromKlks(MerAccountQueryKlksDTO accountQueryDTO) {
        KLog.info("【商户账户查询(快收查凌云)】入参:{}", accountQueryDTO.toJSON());
        UserBaseExt userBaseExt = transactionUserMapper.getUserBaseExtByOtherUserId(accountQueryDTO.getKlksUserId(), PlatNoEnums.KLKS.getCode());
        if (userBaseExt == null) {
            KLog.warn("【商户账户查询(快收查凌云)】获取凌云userId异常,入参:{}", accountQueryDTO.toJSON());
            return Result.builder().fail(BizCodeMessages.UNKNOWN_ERROR, "获取凌云userId异常");
        }

        MerAccountQueryDTO merAccountQueryDTO = new MerAccountQueryDTO();
        merAccountQueryDTO.setPlatNo(PlatNoEnums.KLKS.getCode());
        merAccountQueryDTO.setUserBaseId(userBaseExt.getUserBaseId());
        return queryMerAccountGroup(merAccountQueryDTO);
    }

    private boolean fmtFundQueryGroup(Integer fullAccountType) {
        if (PlatNoEnums.YXY.isEquals(Short.valueOf(StringUtils.substring(fullAccountType.toString(), 0, 2)))) {
            if (AccountType.ALLY.isEqualsFullAccountNo(fullAccountType)) {
                return true;
            } else if (AccountType.ALLY_REACH.isEqualsFullAccountNo(fullAccountType)) {
                return acmTransactionParams.getAllyReachFlagYxy();
            } else if (AccountType.CREDIT_COMMISSION.isEqualsFullAccountNo(fullAccountType)) {
                return acmTransactionParams.getCreditFlagYxy();
            } else if (AccountType.SHOPPING_SCORE.isEqualsFullAccountNo(fullAccountType)) {
                return acmTransactionParams.getShoppingFlagYxy();
            } else if (AccountType.ALLY_PROFIT.isEqualsFullAccountNo(fullAccountType)) {
                return acmTransactionParams.getAllyProfigFlagYxy();
            } else if (AccountType.CONVERSION.isEqualsFullAccountNo(fullAccountType)) {
                return acmTransactionParams.getConversionFlagYxy();
            } else if (AccountType.SUB_TEAM_ACTION.isEqualsFullAccountNo(fullAccountType)) {
                return acmTransactionParams.getSubTeamFlagYxy();
            } else if (AccountType.ALLY_V2.isEqualsFullAccountNo(fullAccountType)) {
                return true;
            } else if (AccountType.ALLY_FROM_BOSS.isEqualsFullAccountNo(fullAccountType)) {
                return true;
            } else if (AccountType.MERCHANT_FROM_BOSS.isEqualsFullAccountNo(fullAccountType)) {
                return true;
            } else if (AccountType.MER.isEqualsFullAccountNo(fullAccountType)) {
                return true;
            }
        }

        if (AccountType.ALLY.isEqualsFullAccountNo(fullAccountType)) {
            return true;
        } else if (AccountType.ALLY_REACH.isEqualsFullAccountNo(fullAccountType)) {
            return acmTransactionParams.getAllyReachFlag();
        } else if (AccountType.CREDIT_COMMISSION.isEqualsFullAccountNo(fullAccountType)) {
            return acmTransactionParams.getCreditFlag();
        } else if (AccountType.SHOPPING_SCORE.isEqualsFullAccountNo(fullAccountType)) {
            return acmTransactionParams.getShoppingFlag();
        } else if (AccountType.ALLY_PROFIT.isEqualsFullAccountNo(fullAccountType)) {
            return acmTransactionParams.getAllyProfigFlag();
        } else if (AccountType.CONVERSION.isEqualsFullAccountNo(fullAccountType)) {
            return acmTransactionParams.getConversionFlag();
        } else if (AccountType.SUB_TEAM_ACTION.isEqualsFullAccountNo(fullAccountType)) {
            return acmTransactionParams.getSubTeamFlagYxy();
        }
        return false;
    }

    /**
     * 用户账户查询(快收查凌云)
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<String> queryAccountFromKlks(String param) {

        KLog.info("【用户账户查询(快收查凌云)】入参:{}", param);

        JSONObject jbc = JSON.parseObject(param);
        if (StringUtils.isBlank(jbc.getString("data"))) {
            KLog.warn("【用户账户查询(快收查凌云)】快收传入数据异常,入参:{}", param);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "快收传入数据异常");
        }

        String decryptStr = AesEncryptUtil.base64AesDecrypt(jbc.getString("data"));
        KLog.info("【用户账户查询(快收查凌云)】解密后的数据:{}, 入参:{}", decryptStr, param);

        JSONObject userJbc = JSON.parseObject(decryptStr);
        String kuaishouUserId = userJbc.getString("userId");
        Integer accountType = userJbc.getInteger("accountType");
        if (StringUtils.isBlank(kuaishouUserId) || accountType == null) {
            KLog.warn("【用户账户查询(快收查凌云)】快收传入数据异常,解密后的数据:{}", decryptStr);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "快收传入数据异常");
        }

        if (StringUtils.isBlank(kuaishouUserId)) {
            KLog.warn("【用户账户查询(快收查凌云)】获取快收userId异常,入参:{}", param);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "获取快收userId异常");
        }

        UserBaseExt userBaseExt = transactionUserMapper.getUserBaseExtByOtherUserId(kuaishouUserId, PlatNoEnums.KLKS.getCode());
        if (userBaseExt == null) {
            KLog.warn("【用户账户查询(快收查凌云)】获取凌云userId异常,解密后的数据:{}", decryptStr);
            return Result.builder().fail(BizCodeMessages.UNKNOWN_ERROR, "获取凌云userId异常");
        }

        LingYunAccVO vo = new LingYunAccVO();

        //用户账户
        FundAccount userAccount = transactionFundAccountMapper.getUserAccountByType(userBaseExt.getUserBaseId(), accountType);
        if (userAccount == null) {
            KLog.warn("【用户账户查询(快收查凌云)】未找到用户账户,解密后的数据:{},lingyunUserBaseId:{}", decryptStr, userBaseExt.getUserBaseId());
            return Result.builder().success(AesEncryptUtil.base64AesEncrypt(vo.toJSON()));
        }

        vo.setAccountWithdrawable(userAccount.getAccountAmountWithdrawable());
        vo.setAccountWithdrawed(userAccount.getAccountAmountWithdrawed());

        KLog.info("【用户账户查询(快收查凌云)】返回加密前数据:{}, 解密后的数据:{}", vo.toJSON(), decryptStr);
        return Result.builder().success(AesEncryptUtil.base64AesEncrypt(vo.toJSON()));
    }

    /**
     * 用户账户批量查询(快收查凌云)
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<String> batchQueryAccountFromKlks(String param) {

        KLog.info("【用户账户批量查询(快收查凌云)】入参:{}", param);

        JSONObject jbc = JSON.parseObject(param);
        if (StringUtils.isBlank(jbc.getString("data"))) {
            KLog.warn("【用户账户批量查询(快收查凌云)】快收传入数据异常,入参:{}", param);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "快收传入数据异常");
        }

        String decryptStr = AesEncryptUtil.base64AesDecrypt(jbc.getString("data"));
        KLog.info("【用户账户批量查询(快收查凌云)】解密后的数据:{}, 入参:{}", decryptStr, param);

        JSONObject userJbc = JSON.parseObject(decryptStr);
        String kuaishouUserId = userJbc.getString("userId");
        String accountTypes = userJbc.getString("accountTypes");     //使用 "|" 隔开
        if (StringUtils.isAnyBlank(kuaishouUserId, accountTypes)) {
            KLog.warn("【用户账户批量查询(快收查凌云)】快收传入数据异常,解密后的数据:{}", decryptStr);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "快收传入数据异常");
        }

        if (StringUtils.isBlank(kuaishouUserId)) {
            KLog.warn("【用户账户批量查询(快收查凌云)】获取快收userId异常,入参:{}", param);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "获取快收userId异常");
        }

        UserBaseExt userBaseExt = transactionUserMapper.getUserBaseExtByOtherUserId(kuaishouUserId, PlatNoEnums.KLKS.getCode());
        if (userBaseExt == null) {
            KLog.warn("【用户账户批量查询(快收查凌云)】获取凌云userId异常,解密后的数据:{}", decryptStr);
            return Result.builder().fail(BizCodeMessages.UNKNOWN_ERROR, "获取凌云userId异常");
        }

        Map<Integer, LingYunAccVO> batchAcc = new HashMap<>();
        //用户账户
        for (String accountType : StringUtils.split(accountTypes, "|")) {
            FundAccount userAccount = transactionFundAccountMapper.getUserAccountByType(userBaseExt.getUserBaseId(), Integer.parseInt(accountType));
            if (userAccount != null) {
                LingYunAccVO accVO = new LingYunAccVO();
                accVO.setAccountWithdrawable(userAccount.getAccountAmountWithdrawable());
                accVO.setAccountWithdrawed(userAccount.getAccountAmountWithdrawed());
                batchAcc.put(Integer.parseInt(accountType), accVO);
            }
        }

        KLog.info("【用户账户批量查询(快收查凌云)】返回加密前数据:{}, 解密后的数据:{}", batchAcc, decryptStr);
        return Result.builder().success(AesEncryptUtil.base64AesEncrypt(JSON.toJSONString(batchAcc)));
    }

    /**
     * 快收用户账户查询
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<AccountQueryVO> queryAllKlks(KlksAccountQueryDTO accountQueryDTO) {
        KLog.info("【快收用户账户查询】入参:{}", accountQueryDTO.toJSON());
        UserBaseExt userBaseExt = transactionUserMapper.getUserBaseExtByOtherUserId(accountQueryDTO.getKlksUserId(), PlatNoEnums.KLKS.getCode());
        AccountQueryDTO queryDTO = new AccountQueryDTO();
        queryDTO.setUserBaseId(userBaseExt.getUserBaseId());
        queryDTO.setPlatNo(PlatNoEnums.KLKS.getCode());
        return queryAccount(queryDTO);
    }

    /**
     * 快收用户账户查询转发
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<String> queryAllKlksForward(String param) {
        KLog.info("【快收用户账户查询转发】入参:{}", param);

        JSONObject jbc = JSON.parseObject(param);
        if (StringUtils.isBlank(jbc.getString("data"))) {
            KLog.warn("【快收用户账户查询转发】快收传入数据异常,入参:{}", param);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "快收传入数据异常");
        }

        String decryptStr = AesEncryptUtil.base64AesDecrypt(jbc.getString("data"));
        KLog.info("【快收用户账户查询转发】解密后的数据:{}, 入参:{}", decryptStr, param);

        JSONObject userJbc = JSON.parseObject(decryptStr);
        String kuaishouUserId = userJbc.getString("userId");
        if (StringUtils.isBlank(kuaishouUserId)) {
            KLog.warn("【快收用户账户查询转发】获取快收userId异常,入参:{}", param);
            return Result.builder().fail(BizCodeMessages.VALIDATOR_ERR, "获取快收userId异常");
        }

        UserBaseExt userBaseExt = transactionUserMapper.getUserBaseExtByOtherUserId(kuaishouUserId, PlatNoEnums.KLKS.getCode());
        if (userBaseExt == null) {
            KLog.warn("【快收用户账户查询转发】获取凌云userId异常,解密后的数据:{}", decryptStr);
            return Result.builder().fail(BizCodeMessages.UNKNOWN_ERROR, "获取凌云userId异常");
        }

        AccountQueryDTO accountQueryDTO = new AccountQueryDTO();
        accountQueryDTO.setPlatNo(PlatNoEnums.KLKS.getCode());
        accountQueryDTO.setUserBaseId(userBaseExt.getUserBaseId());
        AccountQueryVO accountQueryVO;
        Result<AccountQueryVO> queryAccountResult = queryAccount(accountQueryDTO);
        if (queryAccountResult.isSuccess()) {
            accountQueryVO = queryAccountResult.getData();
        } else {
            accountQueryVO = new AccountQueryVO();
        }
        KLog.info("【快收用户账户查询转发】返回加密前数据:{}, 解密后的数据:{}", accountQueryVO.toJSON(), decryptStr);
        return Result.builder().success(AesEncryptUtil.base64AesEncrypt(accountQueryVO.toJSON()));
    }

    /**
     * 盟友首页收益
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<AllyHomePageVO> allyHomePage(AllyHomePageDTO allyHomePageDTO) {
        KLog.info("【盟友首页收益】入参:{}", allyHomePageDTO.toJSON());
        AllyHomePageVO allyHomePageVO = new AllyHomePageVO();
        if (PlatNoEnums.KLKS.isEquals(allyHomePageDTO.getPlatNo())) {
            //返现收益账户
            List<Integer> cashEarningsAccountType = PayAccountConfig.accountTypeCashEarnings(allyHomePageDTO.getPlatNo());
            List<Short> cashEarningsTypes = Arrays.asList(FundBillType.TYPE_110.getType(), FundBillType.TYPE_113.getType(),
                    FundBillType.TYPE_140.getType(), FundBillType.TYPE_160.getType(), FundBillType.TYPE_130.getType(),
                    FundBillType.TYPE_460.getType());

            allyHomePageVO.setTodayCashEarnings(
                    transactionFundBillMapper.todayEarnings(
                            allyHomePageDTO.getPlatNo(), allyHomePageDTO.getUserBaseId(), DateFormat.DATE_COMPACT.format(LocalDateTime.now()),
                            cashEarningsAccountType, cashEarningsTypes));
            allyHomePageVO.setMonthCashEarnings(
                    transactionFundBillMapper.monthEarnings(
                            allyHomePageDTO.getPlatNo(), allyHomePageDTO.getUserBaseId(), DateFormat.DATE_MONTH.format(LocalDateTime.now()),
                            cashEarningsAccountType, cashEarningsTypes));

            //推卡收益账户
            List<Integer> creditEarningsAccountType = PayAccountConfig.accountTypeCreditEarnings(allyHomePageDTO.getPlatNo());
            List<Short> creditEarningsTypes = Arrays.asList(FundBillType.TYPE_120.getType());

            allyHomePageVO.setTodayCreditEarnings(
                    transactionFundBillMapper.todayEarnings(
                            allyHomePageDTO.getPlatNo(), allyHomePageDTO.getUserBaseId(), DateFormat.DATE_COMPACT.format(LocalDateTime.now()),
                            creditEarningsAccountType, creditEarningsTypes));
            allyHomePageVO.setMonthCreditEarnings(
                    transactionFundBillMapper.monthEarnings(
                            allyHomePageDTO.getPlatNo(), allyHomePageDTO.getUserBaseId(), DateFormat.DATE_MONTH.format(LocalDateTime.now()),
                            creditEarningsAccountType, creditEarningsTypes));
        } else if (PlatNoEnums.YXY.isEquals(allyHomePageDTO.getPlatNo())) {
            //返现收益账户
            List<Integer> cashEarningsMainJournalType = Arrays.asList(AccountMainJournalType.ALLY_ACTIVATE.getCode(), AccountMainJournalType.ALLY_REACH.getCode());
            List<Short> cashEarningsTypes = Arrays.asList(FundBillType.TYPE_110.getType(), FundBillType.TYPE_113.getType(),
                    FundBillType.TYPE_140.getType(), FundBillType.TYPE_160.getType(), FundBillType.TYPE_130.getType(),
                    FundBillType.TYPE_450.getType(), FundBillType.TYPE_460.getType());

            allyHomePageVO.setTodayCashEarnings(
                    transactionFundBillMapper.todayEarningsByMainJournalType(
                            allyHomePageDTO.getUserBaseId(), DateFormat.DATE_COMPACT.format(LocalDateTime.now()), cashEarningsMainJournalType, cashEarningsTypes));
            allyHomePageVO.setMonthCashEarnings(
                    transactionFundBillMapper.monthEarningsByMainJournalType(
                            allyHomePageDTO.getUserBaseId(), DateFormat.DATE_MONTH.format(LocalDateTime.now()), cashEarningsMainJournalType, cashEarningsTypes));

            //推卡收益账户
            List<Integer> creditEarningsMainJournalType = Collections.singletonList(AccountMainJournalType.CREDIT_CARD.getCode());
            List<Short> creditEarningsTypes = Arrays.asList(FundBillType.TYPE_120.getType(), FundBillType.TYPE_450.getType());

            allyHomePageVO.setTodayCreditEarnings(
                    transactionFundBillMapper.todayEarningsByMainJournalType(
                            allyHomePageDTO.getUserBaseId(), DateFormat.DATE_COMPACT.format(LocalDateTime.now()), creditEarningsMainJournalType, creditEarningsTypes));
            allyHomePageVO.setMonthCreditEarnings(
                    transactionFundBillMapper.monthEarningsByMainJournalType(
                            allyHomePageDTO.getUserBaseId(), DateFormat.DATE_MONTH.format(LocalDateTime.now()), creditEarningsMainJournalType, creditEarningsTypes));
        }

        KLog.info("【盟友首页收益】出参:{}, 入参:{}", allyHomePageVO.toJSON(), allyHomePageDTO.toJSON());
        return Result.builder().success(allyHomePageVO);
    }

    /**
     * 快收盟友首页收益
     */
    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<AllyHomePageVO> allyHomePageFromKlks(KlksAllyHomePageDTO klksAllyHomePageDTO) {
        KLog.info("【快收盟友首页收益】入参:{}", klksAllyHomePageDTO.toJSON());
        UserBaseExt userBaseExt = transactionUserMapper.getUserBaseExtByOtherUserId(klksAllyHomePageDTO.getKlksUserId(), PlatNoEnums.KLKS.getCode());
        AllyHomePageDTO allyHomePageDTO = new AllyHomePageDTO();
        allyHomePageDTO.setUserBaseId(userBaseExt.getUserBaseId());
        allyHomePageDTO.setPlatNo(PlatNoEnums.KLKS.getCode());
        return allyHomePage(allyHomePageDTO);
    }

    @RoutingDataSource(DataSources.SLAVE_DB)
    @Override
    public Result<List<AllyAccountVO>> queryAllyAccountList(AccountQueryDTO data) {
        //所有收益
        List<AllyAccountVO> totalIncomeList = transactionFundBillMapper.queryAllyAccountIncomeByTime(data.getUserBaseId(), null);
        //今日收益
        List<AllyAccountVO> todayIncomeList = transactionFundBillMapper.queryAllyAccountIncomeByTime(data.getUserBaseId(), DateUtil.getDate());

        AllyAccountVO activeRewardAccount = new AllyAccountVO("返现收益", AccountType.ALLY_FROM_BOSS.getFullCode(PlatNoEnums.YXY.getCode()));
        AllyAccountVO profitAccount = new AllyAccountVO("分润收益", AccountType.ALLY_FROM_BOSS.getFullCode(PlatNoEnums.YXY.getCode()));
        AllyAccountVO otherRewardAccount = new AllyAccountVO("其他收益", AccountType.ALLY_FROM_BOSS.getFullCode(PlatNoEnums.YXY.getCode()));
        AllyAccountVO bossReversalAccount = new AllyAccountVO("奖励冲正", AccountType.ALLY_FROM_BOSS.getFullCode(PlatNoEnums.YXY.getCode()));

        AllyAccountVO activityRewardAccount = new AllyAccountVO("活动奖励", AccountType.ALLY_V2.getFullCode(PlatNoEnums.YXY.getCode()));
        AllyAccountVO platReversalAccount = new AllyAccountVO("分红冲正", AccountType.ALLY_V2.getFullCode(PlatNoEnums.YXY.getCode()));


        for (AllyAccountVO allyAccountVO : totalIncomeList) {

            if (AccountType.ALLY_FROM_BOSS.isEqualsFullAccountNo(allyAccountVO.getAccountType())) {

                if (AccountMainJournalType.ALLY_REACH.isEquals(allyAccountVO.getAccountMainJournalType())
                        || AccountMainJournalType.ALLY_ACTIVATE.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    activeRewardAccount.setTotalIncome(activeRewardAccount.getTotalIncome().add(allyAccountVO.getTotalIncome().setScale(2, RoundingMode.HALF_UP)));

                } else if (AccountMainJournalType.ALLY_PROFIT.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    profitAccount.setTotalIncome(profitAccount.getTotalIncome().add(allyAccountVO.getTotalIncome().setScale(2, RoundingMode.HALF_UP)));

                } else if (AccountMainJournalType.ALLY_REFUND.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    bossReversalAccount.setTotalIncome(bossReversalAccount.getTotalIncome().add(allyAccountVO.getTotalIncome().setScale(2, RoundingMode.HALF_UP)));

                } else {

                    otherRewardAccount.setTotalIncome(otherRewardAccount.getTotalIncome().add(allyAccountVO.getTotalIncome().setScale(2, RoundingMode.HALF_UP)));
                }

            } else if (AccountType.ALLY_V2.isEqualsFullAccountNo(allyAccountVO.getAccountType())) {

                // 加帐冲正
                if (AccountMainJournalType.ALLY_REFUND.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    platReversalAccount.setTotalIncome(platReversalAccount.getTotalIncome().add(allyAccountVO.getTotalIncome().setScale(2, RoundingMode.HALF_UP)));

                } else {

                    activityRewardAccount.setTotalIncome(activityRewardAccount.getTotalIncome().add(allyAccountVO.getTotalIncome().setScale(2, RoundingMode.HALF_UP)));
                }
            }
        }

        for (AllyAccountVO allyAccountVO : todayIncomeList) {

            if (AccountType.ALLY_FROM_BOSS.isEqualsFullAccountNo(allyAccountVO.getAccountType())) {

                if (AccountMainJournalType.ALLY_REACH.isEquals(allyAccountVO.getAccountMainJournalType())
                        || AccountMainJournalType.ALLY_ACTIVATE.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    activeRewardAccount.setTodayIncome(activeRewardAccount.getTodayIncome().add(allyAccountVO.getTodayIncome().setScale(2, RoundingMode.HALF_UP)));

                } else if (AccountMainJournalType.ALLY_PROFIT.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    profitAccount.setTodayIncome(profitAccount.getTodayIncome().add(allyAccountVO.getTodayIncome().setScale(2, RoundingMode.HALF_UP)));

                } else if (AccountMainJournalType.ALLY_REFUND.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    bossReversalAccount.setTodayIncome(bossReversalAccount.getTodayIncome().add(allyAccountVO.getTodayIncome().setScale(2, RoundingMode.HALF_UP)));

                } else {

                    otherRewardAccount.setTodayIncome(otherRewardAccount.getTodayIncome().add(allyAccountVO.getTodayIncome().setScale(2, RoundingMode.HALF_UP)));
                }
            } else if (AccountType.ALLY_V2.isEqualsFullAccountNo(allyAccountVO.getAccountType())) {

                // 加帐冲正
                if (AccountMainJournalType.ALLY_REFUND.isEquals(allyAccountVO.getAccountMainJournalType())) {

                    platReversalAccount.setTodayIncome(platReversalAccount.getTodayIncome().add(allyAccountVO.getTodayIncome().setScale(2, RoundingMode.HALF_UP)));

                } else {

                    activityRewardAccount.setTodayIncome(activityRewardAccount.getTodayIncome().add(allyAccountVO.getTodayIncome().setScale(2, RoundingMode.HALF_UP)));
                }
            }

        }

        List<AllyAccountVO> accountVOList = new ArrayList<>();
        accountVOList.add(activeRewardAccount);
        accountVOList.add(profitAccount);
        accountVOList.add(activityRewardAccount);
        accountVOList.add(otherRewardAccount);
        accountVOList.add(platReversalAccount);
        accountVOList.add(bossReversalAccount);

        return Result.builder().success(accountVOList);
    }

    private boolean isA7(String userBaseId, Short platNo) {

        KLog.info("请求获取身份等级rpc:{},{}", userBaseId, platNo);
        if (!PlatNoEnums.YXY.isEquals(platNo)) {
            return false;
        }
        SuperiorAllyDTO allyShareLevelForDate = null;
        try {
            allyShareLevelForDate = allyProfitService.getAllyShareLevelForDate(userBaseId, platNo, ChannelIdEnums.yxy_lkl_p40.getCode(),
                    LocalDateUtils.formatByDefaultFormat(LocalDateTime.now()));
        } catch (Exception e) {
            return true;
        }
        int compare = StringUtils.compare(allyShareLevelForDate.getProfitShareLevelName(), allyShareLevelForDate.getTopLevelName());
        String shareLevelName = "";
        //取较大的等级
        if (compare <= 0) {
            shareLevelName = allyShareLevelForDate.getTopLevelName();
        } else {
            shareLevelName = allyShareLevelForDate.getProfitShareLevelName();
        }

        return StringUtils.equals(ShareLevelBaseEnums.YXY_A7.getShareLevelName(), shareLevelName);
    }
}
